package com.wechat.service.cmds.msg;

import java.util.concurrent.CompletableFuture;

import com.swak.annotation.Flux;
import com.wechat.annotation.Cmd;
import com.wechat.command.Command;
import com.wechat.command.CommandResult;
import com.wechat.command.Commands;
import com.wechat.dto.MessageDTO;
import com.wechat.enums.MessageType;
import com.wechat.face.Faces;
import com.wechat.msg.TxtMsg;
import com.wechat.network.NetworkChannel;

/**
 * 定义命令处理器
 * 
 * @author 618lf
 */
@Flux
@Cmd(command = Commands.Msg_Req_Txt, name = "处理小文本")
public class TxtMsgHandler extends AbstractMsgHandler {

	/**
	 * 处理 文本消息
	 * 
	 * @param command
	 * @return
	 */
	@Override
	public CompletableFuture<CommandResult> handle(NetworkChannel channel, Command command) {

		// debug
		if (logger.isDebugEnabled()) {
			logger.debug("处理消息, {}", command);
		}

		// 转换消息
		TxtMsg msg = command.dataToMsg();

		// 創建消息
		MessageDTO message = MessageDTO.of() // 创建消息
				.setMsgType(MessageType.Txt.getV())// 消息類型
				.setMsgBrief(Faces.parseBriefString(msg.getContent())) // 消息简述
				.setMsgContent(String.valueOf(command.getData())) // 消息内容
				.setTalker(command.getFrom()); // 消息来源

		// 存储消息，返回处理成功
		return this.receive(message, msg).thenApply(res -> CommandResult.SUCCESS);
	}
}
